package io.modifier.basic.compose.widget.weui.core.utils

import androidx.compose.ui.geometry.Offset
import kotlin.math.cos
import kotlin.math.sin

object Math {
    /**
     * Constant by which to multiply an angular value in degrees to obtain an
     * angular value in radians.
     */
    private val DEGREES_TO_RADIANS: Double = 0.017453292519943295

    /**
     * Converts an angle measured in degrees to an approximately
     * equivalent angle measured in radians.  The conversion from
     * degrees to radians is generally inexact.
     *
     * @param   angdeg   an angle, in degrees
     * @return  the measurement of the angle {@code angdeg}
     *          in radians.
     */
    fun toRadians(angdeg: Double): Double {
        return angdeg * DEGREES_TO_RADIANS
    }
}

/**
 * 将极坐标转换为笛卡尔坐标
 */
fun polarToCartesian(
    center: Offset,
    radius: Float,
    angleRadians: Double
): Pair<Float, Float> {
    return Pair(
        center.x + (radius * cos(angleRadians)).toFloat(),
        center.y + (radius * sin(angleRadians)).toFloat()
    )
}

/**
 * 计算贝塞尔曲线
 */
fun calculateBezierPath(start: Offset, end: Offset, progress: Float): Offset {
    /**
     * B(t) = (1 - t)^2 * P0 + 2 * (1 - t) * t * P1 + t^2 * P2
     * 其中，P0、P1 和 P2 分别是起点、控制点和终点，t 是进度参数
     */
    fun formula(p0: Float, p1: Float, p2: Float, t: Float) =
        (1 - t) * (1 - t) * p0 + 2 * (1 - t) * t * p1 + t * t * p2

    val controlPoint = Offset((start.x + end.x) / 2, start.y - 200f)

    return Offset(
        formula(p0 = start.x, p1 = controlPoint.x, p2 = end.x, t = progress),
        formula(p0 = start.y, p1 = controlPoint.y, p2 = end.y, t = progress)
    )
}